//
//  MenuSegmentViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/27.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "XLPageViewController.h"
#import "MenuDetailBottomView.h"
#import "MenuSegTitleCell.h"

#import "MenuSubViewController.h"
#import "V2MenuSubViewController.h"

#import "MessageCenterViewController.h"
#import "StoreHotelDetailViewController.h"

#import "MenuSegmentViewController.h"

@interface MenuSegmentViewController ()<XLPageViewControllerDelegate,XLPageViewControllerDataSrouce>

@property (nonatomic, strong) XLPageViewController *pageViewController;

@property (nonatomic, strong) MenuDetailBottomView *bottomView;

@end

@implementation MenuSegmentViewController

- (void)dealloc {
    NSLog(@"菜单释放了");
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
}

- (void)initView {
    self.title = @"菜单";

    self.view.backgroundColor = UIColorHex(0xf0f0f0);
    
    [self initPageViewController];
    self.pageViewController.selectedIndex = self.selectIndex;

    UIButton *imBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    imBtn.frame = CGRectMake(0,0,40,30);
    [imBtn setImage:[UIImage imageNamed:@"field_xiaoxi"] forState:UIControlStateNormal];
    [imBtn addTarget:self action:@selector(goIm) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *imItem = [[UIBarButtonItem alloc] initWithCustomView:imBtn];

    self.navigationItem.rightBarButtonItems =@[imItem];
    
    [RACObserve([TT_MessageManager manager], totalUnReadCount) subscribeNext:^(id x) {
        if ([TT_MessageManager manager].totalUnReadCount > 0) {
            imBtn.badgeValue = [NSString stringWithFormat:@"%d",[TT_MessageManager manager].totalUnReadCount];
            imBtn.badgeBGColor = [UIColor redColor];
        } else {
            imBtn.badgeValue = @"";
            imBtn.badgeBGColor = [UIColor clearColor];
        }
    }];
}

- (void)goIm {
    MessageCenterViewController *vc = [[MessageCenterViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}


- (void)initPageViewController {
    XLPageViewControllerConfig *config = [XLPageViewControllerConfig defaultConfig];
    //隐藏底部阴影
    config.shadowLineHidden = false;
    //设置标题颜色
    config.titleSelectedColor = UIColorHex(0xff4242);
    //标题正常颜色
    config.titleNormalColor = UIColorHex(0x999999);
    
    config.titleViewBackgroundColor = UIColorHex(0xf7f7f7);
    
    config.shadowLineColor = UIColorHex(0xff4242);

    config.titleNormalFont = [UIFont systemFontOfSize:14];
    config.titleSelectedFont = [UIFont systemFontOfSize:14];

    //设置标题间距
    config.titleSpace = 0;
    //标题宽度
    config.titleWidth = TTScreenWidth/4;
    config.titleViewAlignment = XLPageTitleViewAlignmentCenter;
    

    config.titleViewHeight = 60;
    //设置标题栏缩进
    config.titleViewInset = UIEdgeInsetsMake(0, 0, 0, 0);
    //在导航栏中显示
    config.showTitleInNavigationBar = false;
    //隐藏分割线
    config.separatorLineHidden = true;
    
    self.pageViewController = [[XLPageViewController alloc] initWithConfig:config];
    self.pageViewController.view.frame = self.view.bounds;
    self.pageViewController.delegate = self;
    self.pageViewController.dataSource = self;
    
//    [self.pageViewController registerClass:[MenuSegTitleCell class] forTitleViewCellWithReuseIdentifier:@"MenuSegTitleCellID"];
    
    [self.pageViewController registerNib:[UINib nibWithNibName:@"MenuSegTitleCell" bundle:nil] forTitleViewCellWithReuseIdentifier:@"MenuSegTitleCellID"];
    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];
    [self.view addSubview:self.bottomView];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(60);
        make.bottom.mas_equalTo(-Bottom_SafeHeight);
    }];
    
    [self.pageViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.bottom.mas_equalTo(self.bottomView.mas_top);
    }];
    
    WEAKSELF
    self.bottomView.MenuDetailBottomViewBlock = ^(MenuDetailBottomActionType type) {
      
        switch (type) {
            case MenuDetailBottomActionType_store:{
                StoreHotelDetailViewController *vc = [[StoreHotelDetailViewController alloc] init];
                vc.storeId = weakSelf.menuArray.firstObject.bhmSellerId;
                [weakSelf.navigationController pushViewController:vc animated:YES];
                
            } break;
            case MenuDetailBottomActionType_im:{
                [[TT_IMContactManager manager] ImContactWithStoreId:weakSelf.menuArray.firstObject.bhmSellerId];
            } break;
            case MenuDetailBottomActionType_phone:{
                [[TT_PhonaCallManager manager] callWithStoreId:weakSelf.menuArray.firstObject.bhmSellerId];
            } break;
            case MenuDetailBottomActionType_subscribe:{
                [[BA_BusinessHelper shared] callSubscribeStoreWith:weakSelf.menuArray.firstObject.bhmSellerId];
            } break;
            default:
                break;
        }
    };
}

#pragma mark -
#pragma mark TableViewDelegate&DataSource
- (UIViewController *)pageViewController:(XLPageViewController *)pageViewController viewControllerForIndex:(NSInteger)index {
    
    if (self.menuArray[index].dishesList.count > 0) {
        V2MenuSubViewController *vc = [[V2MenuSubViewController alloc] initWithModel:self.menuArray[index]];
        return vc;
    } else {
        
        if ([self.menuArray[index].bhmMenuJson containsString:@"请升级版本"]) {
            V2MenuSubViewController *vc = [[V2MenuSubViewController alloc] initWithModel:self.menuArray[index]];
            return vc;
        } else {
            MenuSubViewController *vc = [[MenuSubViewController alloc] initWithModel:self.menuArray[index]];
            return vc;
        }
    }
}

- (NSString *)pageViewController:(XLPageViewController *)pageViewController titleForIndex:(NSInteger)index {
    return self.menuArray[index].bhmMenuName;
}

- (NSInteger)pageViewControllerNumberOfPage {
    return self.menuArray.count;
}

- (XLPageTitleCell *)pageViewController:(XLPageViewController *)pageViewController titleViewCellForItemAtIndex:(NSInteger)index {
    MenuSegTitleCell *cell = [pageViewController dequeueReusableTitleViewCellWithIdentifier:@"MenuSegTitleCellID" forIndex:index];
    cell.mainTitle.text = self.menuArray[index].bhmMenuName;
    cell.subTitle.text =  [NSString stringWithFormat:@"¥%@/桌",[Utilities changeDoubleToString:self.menuArray[index].bhmMenuPrice]];

    return cell;
}

- (void)pageViewController:(XLPageViewController *)pageViewController didSelectedAtIndex:(NSInteger)index {

}

- (MenuDetailBottomView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[NSBundle mainBundle] loadNibNamed:@"MenuDetailBottomView"owner:nil options:nil].firstObject;
    }
    return _bottomView;
}

@end
